package my.suveng.generator;

import cn.hutool.core.io.FileUtil;
import freemarker.template.TemplateException;
import my.suveng.generator.entity.ClassInfo;
import my.suveng.generator.entity.ParamInfo;
import my.suveng.generator.service.GeneratorService;
import my.suveng.generator.util.FreemarkerTool;
import my.suveng.generator.util.TableParseUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author suwenguang
 **/
@SpringBootApplication
public class Generator {

    public static void main(String[] args) throws IOException, TemplateException {

        ConfigurableApplicationContext context = SpringApplication.run(Generator.class, args);
        ParamInfo paramInfo = new ParamInfo();
        paramInfo.setTableSql("CREATE TABLE 'userinfo' (\n" +
                "  'user_id' int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',\n" +
                "  'username' varchar(255) NOT NULL COMMENT '用户名',\n" +
                "  'addtime' datetime NOT NULL COMMENT '创建时间',\n" +
                "  PRIMARY KEY ('user_id')\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息'");
        paramInfo.setAuthorName("");
        paramInfo.setPackageName("");
        paramInfo.setReturnUtil("");
        paramInfo.setNameCaseType("");
        paramInfo.setTinyintTransType("");
        paramInfo.setDataType("");
        paramInfo.setSwagger(false);
        // sql转化成class信息
        ClassInfo classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);
        // 配置加载
        Map<String, Object> params = new HashMap<>(8);
        params.put("classInfo", classInfo);
        params.put("tableName", classInfo == null ? System.currentTimeMillis() : classInfo.getTableName());
        params.put("authorName", paramInfo.getAuthorName());
        params.put("packageName", paramInfo.getPackageName());
        params.put("returnUtil", paramInfo.getReturnUtil());
        params.put("swagger", paramInfo.isSwagger());


        FreemarkerTool freemarkerTool = context.getBean(FreemarkerTool.class);
        GeneratorService generator = context.getBean(GeneratorService.class);

        //生成jpa文件
        generator.jpa(params);
        //生成mybatis文件
        generator.mybatis(params);


    }



}
